package com.wei.czz.framework.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wei.czz.common.enums.CommonEnum;
import com.wei.czz.common.po.UserPo;
import com.wei.czz.common.utils.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wei.czz.framework.common.dao.FileChunkFlowDao;
import com.wei.czz.framework.common.entity.FileChunkFlowEntity;
import com.wei.czz.framework.common.service.FileChunkFlowService;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2025-03-23 12:38:54
 * className: FileChunkFlowServiceImpl
 * version: 1.0
 * description:
 */
@Service("fileChunkFlowService")
public class FileChunkFlowServiceImpl extends ServiceImpl<FileChunkFlowDao, FileChunkFlowEntity> implements FileChunkFlowService {

    private static final Logger log = LoggerFactory.getLogger(FileChunkFlowServiceImpl.class);

    @Override
    public void saveOrEdit(FileChunkFlowEntity fileChunkFlow) {
        // 获取操作用户
        UserPo userPo = SecurityUtils.getUser();
        Date date = new Date();

        if (Objects.isNull(fileChunkFlow.getId())) {
            // 新增
            fileChunkFlow.setDeleteStatus(CommonEnum.ZERO.getValue())
                    .setUpdateTime(date)
                    .setUpdateUser(StringUtils.EMPTY)
                    .setUpdateUserId(CommonEnum.ZERO.getLongValue())
                    .setCreateTime(date)
                    .setCreateUser(userPo.getUsername())
                    .setCreateUserId(userPo.getUserId());
            int count = baseMapper.insert(fileChunkFlow);
            log.info("新增切片文件上传记录完成。count={}", count);
        } else {
            // 编辑
            fileChunkFlow.setUpdateTime(date)
                    .setUpdateUser(userPo.getUsername())
                    .setUpdateUserId(userPo.getUserId());
            int count = baseMapper.updateById(fileChunkFlow);
            log.info("更新切片文件上传记录完成。count={}", count);
        }
    }

    @Override
    public FileChunkFlowEntity find(String fileHash, String chunkHash) {

        LambdaQueryWrapper<FileChunkFlowEntity> fileChunkFlowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        fileChunkFlowLambdaQueryWrapper.eq(FileChunkFlowEntity::getFileHash, fileHash)
                .eq(FileChunkFlowEntity::getChunkHash, chunkHash)
                .eq(FileChunkFlowEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        fileChunkFlowLambdaQueryWrapper.last("limit 1");
        // 查询
        List<FileChunkFlowEntity> fileChunkFlowList = baseMapper.selectList(fileChunkFlowLambdaQueryWrapper);
        log.info("查询切片文件上传记录完成。list.size={}", fileChunkFlowList.size());
        if (fileChunkFlowList.isEmpty()) {
            return null;
        }
        return fileChunkFlowList.get(0);
    }

    @Override
    public List<FileChunkFlowEntity> findList(List<String> fileHashList) {
        if (CollectionUtils.isEmpty(fileHashList)) {
            log.info("获取切片文件上传记录，文件哈希值列表为空");
            return Collections.emptyList();
        }

        LambdaQueryWrapper<FileChunkFlowEntity> fileChunkFlowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        fileChunkFlowLambdaQueryWrapper.in(FileChunkFlowEntity::getFileHash, fileHashList)
                .eq(FileChunkFlowEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 查询
        List<FileChunkFlowEntity> fileChunkFlowList = baseMapper.selectList(fileChunkFlowLambdaQueryWrapper);
        log.info("文件哈希值列表查询切片文件上传记录完成。list.size={}", fileChunkFlowList.size());

        return fileChunkFlowList;
    }
}